package com.eds.timeTask;

import com.eds.feign.UserService;
import com.eds.log.LogUtil;
import com.eds.service.AsyncRecommendationService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RecommendationScheduler {
    private static final Logger logger = LogUtil.getLogger(RecommendationScheduler.class);

    @Resource
    private AsyncRecommendationService asyncRecommendationService;
    @Autowired
    private UserService userService;


    //@Scheduled(fixedDelay = 190000) // 延迟 3 分钟（180000 毫秒）


    // 每6小时更新一次热门推荐
    @Scheduled(fixedRate = 6 * 60 * 60 * 1000)
    public void updateHotRecommendations() {
        logger.info("定时任务：更新热门推荐");
        asyncRecommendationService.updateHotRecommendationsAsync();
    }


    @Scheduled(fixedRate = 6 * 60 * 60 * 1000)
    public void updateUserRecommendations() {
        logger.info("定时任务：更新用户热门推荐");
        //获取所有用户
        userService.getAllUser().forEach(user -> {
            asyncRecommendationService.updateUserRecommendationsAsync(user.getUserId());
        });
    }

    @Scheduled(fixedRate = 6 * 60 * 60 * 1000)
    public void updateHappyRecommendations() {
        logger.info("定时任务：更新热门推荐");

        asyncRecommendationService.updateHappyRecommendationsAsync();
    }


}